package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 *
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    /**
     * 查询线索跟进记录列表
     *
     * @param clueId
     * @return
     */
    @Override
    public TableDataInfo findPage(Long clueId) {
        TableDataInfo dataInfo = new TableDataInfo();
        List<TbClueTrackRecord> clueTrackRecords = tbClueTrackRecordMapper.findClueTrackRecords(clueId);
        Integer integer = tbClueTrackRecordMapper.ClueTrackRecordsNum(clueId);
        dataInfo.setTotal(integer);
        dataInfo.setRows(clueTrackRecords);
        return dataInfo;
    }

    /**
     * 线索跟进记录表新增
     *
     * @param clueTrackRecordVo
     * @return
     */
    @Override
    public Integer addClueTrackRecords(ClueTrackRecordVo clueTrackRecordVo) {
        // 先进行线索表的修改
        TbClue tbClue = new TbClue();
        BeanUtils.copyProperties(clueTrackRecordVo,tbClue);
        tbClue.setId(clueTrackRecordVo.getClueId());
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbClue.setUpdateTime(new Date());
        tbClue.setUpdateBy(SecurityUtils.getUsername());
        tbClueMapper.updateTbClue(tbClue);
        // 线索表修改完成后,进行跟进记录表的添加
        return  tbClueTrackRecordMapper.addClueTrackRecord(clueTrackRecordVo, SecurityUtils.getUsername(), new Date());
    }
}
